/*
 * @Description:
 * @Autor: luojie
 * @Date: 2021-07-03 08:59:38
 * @LastEditors: luojie
 * @LastEditTime: 2021-08-17 17:12:20
 */
import request from "../request/request";
import {dealPhoneNum} from "../utils";
// 登录接口
export function login (baseUrl, params,fn,shenainin) {
  let phone = params.phone;
  userCheck({phone}).then((res) => {
    console.log("是否是隔离人员："+res.isolateUser)
    console.log("是否是管理员："+res.systemUser)
    uni.setStorage({key:'reportType',data:res})
    let telPhone=""
    let isolateUser=res.isolateUser
    if(isolateUser){
      telPhone="19988886666"
    }else{
      telPhone=phone
    }
    let loginData={
      phone: dealPhoneNum(telPhone),
      timestamp: Date.parse(new Date())
    }
    params.hotelId?loginData.hotelId = params.hotelId:''
    uni.request({
      url: `${baseUrl}/user/login`, //仅为示例，并非真实接口地址。
      method: 'POST',
      data: loginData,
      header:{
        'x-token': '1234567890',
        // "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        let cardId = uni.getStorageSync('cardId');
        if (res.data.code == 200) {
          uni.setStorage({ key: 'token', data: res.data.data.sessionid});
          uni.setStorage({ key: 'personalInfo', data: res.data.data});

          if(cardId){
            console.log("ipad用户登陆");
            //ipad用户证件号验证
            ipadVerify(cardId);
          }else{
            //非ipad用户
            uni.removeStorageSync('cardId');
            if(isolateUser){
              console.log("隔离人员")
              uni.reLaunch({
                url:'/packageIsolate/pages/isolateUser/index',
              })
            }else{
              if(shenainin){
                console.log("深i您")
                uni.reLaunch({
                  url:'/pages/index/index',
                })
              }else{
                console.log("普通管理人员")
                uni.switchTab({
                  url: '/pages/index/index',
                });
              }
            }
          }

          fn&&fn();
        }else if (res.data.code === 401 && !cardId) {
          uni.removeStorage("token");
          uni.setStorage({key:'telPhone',data:''})
          // let page = getCurrentPages().pop();
          uni.showToast({
            title: res.data.msg,
            duration: 2000,
            icon:'none'
          });
          let page = getCurrentPages().pop();
          if(page.route!="pages/login/index"){
            uni.reLaunch({
              url:'/pages/login/index'
            })
          }
        }else {
          uni.showToast({
            title: res.data.msg,
            duration: 3000,
            icon:'none'
          });
        }
      }
    });
  })
}

//ipad用户登陆后的证件验证
export function ipadVerify(cardId){
  getData('getGzryInfo','GET',{}).then(res=>{
    //验证证件后4位是否一致
    var card =res.idCard.substring(res.idCard.length-4);
    if( cardId == card){
      uni.switchTab({
        url: '/pages/index/index',
      });
    }else{
      uni.showToast({
        title: "身份验证错误" ,
        duration: 3000,
        icon:'none'
      });
      //证件号不一致，请求接口登出，清除缓存
      logout().then(rs=>{
        uni.removeStorageSync('isolationPersonnel');
        uni.removeStorageSync('telPhone');
        uni.removeStorageSync('token');
        uni.removeStorageSync('personalInfo');
        uni.removeStorageSync('personKey');
        uni.removeStorageSync('cardId');
      })
    }
  })
}

// 获取深i您手机号码接口
export function getShenAiNinCode (baseUrl, params) {
  uni.request({
    url: `${baseUrl}/shen-ai-nin/user`,
    method: 'get',
    data: params,
    header:{
      'x-token': '1234567890',
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success: (res) => {
      if (res.data.code == 200) {
        uni.setStorage({key:'idNum',data:res.data.data.idNum})
        uni.setStorage({key:'telPhone',data:res.data.data.mobileNum})
        let phone=res.data.data.mobileNum;
        let shenainin = true
        let fn ='';
        login(`${baseUrl}`,{phone:phone, timestamp: Date.parse(new Date())},fn,shenainin);

      }else{
        uni.showToast({
          title: res.data.msg ,
          duration: 3000,
          icon:'none'
        });
      }
    }
  });
}

// 登出接口
export function logout () {
  return request({
    url: '/user/logout',
    method: 'POST'
  })
}

// 根据房间编码查询住房人员
export function houseInfo(houseId) {
  return request({
    url: `/quickly-check-in/person/house-info/${houseId}`,
    method: 'get'
  })
}

// 根据房间编码查询住房人员（新接口：2021.08.12）
export function houseInfoDetail(params) {
  return request({
    url: `/hotel/house/person-info`,
    method: 'POST',
    data: params
  })
}

// 根据房间内人员personId查是否已转诊就医
export function getPersonReferral(personIds) {
  return request({
    url: `/see-doctor/list/state`,
    method: 'POST',
    data: personIds
  })
}

// 根据隔离人员id，查询最近的转诊就医记录
export function getReferralRecord(personId) {
  return request({
    url: `/see-doctor/latest?personId=${personId}`,
    method: 'get'
  })
}

// 删除上传的图片
export function deleteUploadImg(fileId) {
  return request({
    url: `/files/delete?fileId=${fileId}`,
    method: 'DELETE'
  })
}

export function checkInList(data) {
  return request({
    url: `/quickly-check-in/batch`,
    method: 'POST',
    data
  })
}
// 调用接口方法
// 获取省市区
export function getProvinceCity(){
  return request({
    url: '/check-out/province-city',
    method: 'GET'
  })
}
// 通过身份证获取用户信息
export function getUserInfoByCardId(data){
  return request({
    url: `/person-info`,
    method: 'GET',
    data
  })
}

// 获取街道信息
export function getStreetList(data){
  return request({
    url: `/check-out/division/tree`,
    method: 'GET',
    data
  })
}


// 离店
export function checkOut(data){
  return request({
    url: `/check-out`,
    method: 'POST',
    data
  })
}

//批量离店
export function checkOutBatch(data){
  return request({
    url: `/check-out/batch`,
    method: 'POST',
    data
  })
}

// 医学观察
// 获取
export function getMedicalObservation(data){
  return request({
    url: `/medical-observation`,
    method: 'GET',
    data
  })
}
//检查是管理员还是隔离人员
export function userCheck(params) {
  return request({
    url: `/person-info/user-check`,
    method: 'POST',
    data:params
  })
}
// 提交
export function updateMedicalObservation(data, ischange){
  let method = 'POST';
  if(ischange == '1') {
    method = 'PUT';
  }
  return request({
    url: `/medical-observation`,
    method,
    data
  })
}

// 心理评估自主申报
export function mentalHealth(data){
  return request({
    url: `/health-check/person-risk-assessment`,
    method: 'POST',
    header:{
      'x-token': '1234567890',
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data
  })
}
//  巡检消杀 获取
export function getdislist(data){
  return request({
    url: `/goods`,
    method: 'GET',
    data
  })
}
// 获取字典
export function getDic(type){
  return request({
    url: `/tools/dictionary`,
    method: 'GET',
    data:{ type }
  })
}

//  获取工作人员自我上报分页信息
export function getReportList(data){
  return request({
    url: `/report/page`,
    method: 'GET',
    data
  })
}

//  查询健康登记信息列表
export function getJkdjList(data){
  return request({
    url: `/public-report/person-health-investigate/page`,
    method: 'POST',
    data
  })
}

//  公众人员健康登记信息查询
export function getPersonHealth(data){
  return request({
    url: `/public-report/person-health-investigate/latest`,
    method: 'POST',
    data
  })
}
// 公众人员新增健康登记信息
export function setPersonHealth(data){
  return request({
    url: `/public-report/person-health-investigate`,
    method: 'POST',
    data
  })
}

//  查询心理评估信息分页列表
export function getXlpgList(data){
  return request({
    url: `/public-report/person-risk-assessment/list`,
    method: 'POST',
    data
  })
}

// 匹配地址库
export function getAddrDic(data){
  return request({
    url: `/tools/address-info`,
    method: 'GET',
    data
  })
}
// 工作人员自主上报
// 获取
export function getGzryInfo(){
  return request({
    url: `/user/info`,
    method: 'GET',
  })
}

// 工作人员自主上报
// 提交
export function updateGzryData(data){
  return request({
    url: `/report`,
    method: 'POST',
    data,
  })
}

export function getRecordList(data){
  return request({
    url: `/order/finish/list`,
    method: 'POST',
    data,
  })
}

//  查询隔离人员日常监测数据
export function getDailyReportList(data){
  return request({
    url: `/person-info/daily-report-info`,
    method: 'GET',
    data
  })
}

// 政策指引列表
export function getPolicyList(data) {
  return request({
    url: `/book-policy/page`,
    method: 'GET',
    data
  })
}

// 政策指引详情
export function getPolicyDetail(id) {
  return request({
    url: `/book-policy/` + id,
    method: 'GET',
  })
}

// 业务培训列表
export function getTrainingList(data) {
  return request({
    url: `/book-training/page`,
    method: 'GET',
    data
  })
}

// 业务培训详情
export function getTrainingDetail(id) {
  return request({
    url: `/book-training/` + id,
    method: 'GET',
  })
}

// 感染防控督导题目
export function getSupervisionQuestion(data) {
  return request({
    url: `/supervision/question`,
    method: 'GET',
    data
  })
}

// 新增感染防控督导
export function addSupervision(data){
  return request({
    url: `/supervision/insert`,
    method: 'POST',
    data,
  })
}

// 感染防控督导记录
export function getSupervisionList(data){
  return request({
    url: `/supervision/page`,
    method: 'POST',
    data,
  })
}

let api = {
  getUser: "/qr-code/get-user", //扫码获取二维码身份信息
  getPersonInfo:"/person-info", //扫码获取二维码身份信息
  codeInfo:'/tools/code-info', // 扫一码通获取信息、查询离点去向
  roomInfo:"/quickly-check-in/room-list",// 可选房间信息,
  publicRoomList: "/panel/hotel", // 除快捷入住外的选房间
  checkIn: "/check-in", // 入住登记
  healthCheck: "/health-check/person-health-investigate", // 健康排查
  publicCid: "/public-report/cid", // 查询健康排查最新记录，可返回证件号
  comprehensive:'/health-check/person-risk-assessment',   // 心理评估
  userInfo: "/user/user-info", //获取用户详细信息
  getMedicalObservation:'/medical-observation',// 查询医学观察记录
  saveMedicalObservation:'/medical-observation',// 新增医学观察记录
  personInfo: '/person-info',// 扫码后获取隔离人员
  getGzryInfo: '/user/info',// 扫码后获取隔离人员
  updateGzryData: '/report',// 扫码后获取隔离人员
  disinfection: '/goods/houses/disinfection',// 房间消杀
  goods: '/goods', // 物品级别消杀
  houseClean: '/goods/houses/clearn', // 房间保洁
  allDictionary: '/tools/all-dictionary', // 房间保洁
  divisionTree: '/tools/division/tree', // 区划
  addToolPerson: '/tools/person/save',
  addReferralPerson: '/see-doctor',  //添加转诊就医记录
  toolsHotel: '/tools/hotels',  // 隔离点列表
  riskAss: '/public-report/person-risk-assessment',  // 上传心理综合评估信息
  latest: '/public-report/person-risk-assessmen/latest', // 查询24小时内最新心理评估记录
  faceCollSave: '/face-collect/save', // 头像采集
  faceLastRecord: '/face-collect/last-record',  // 查询头像采集记录
  faceComplateRecord: '/face-collect/collect-status',  // 查询头像采集是否已完成
  samplePage: '/hotel-sample/page',  // 获取隔离点核酸采样分页结果
  hotel: '/hotel', // 获取隔离点信息
  hotelAdmin: '/supervision/hotel-admin-user', // 获取隔离点负责人
  businessTraining: '/book-training/type/list', // 业务培训分类
  getTrainingList: '/book-training/page', // 业务培训列表
  isolationDelay: '/overdue-regi/save', // 人员隔离延期
  moveChange: '/move-info/move-change', // 入住变更
  medicalObservationRoom:'/hotel/floor-house/panel', // 医学观察房间面板
  inspectSave: '/inspect-info/save',  // 保存每日巡诊信息
  getSupervision: '/supervision',     // 感染防控督导详情
};
export function getData (url,type,params) {
  return request({
    url: api[url],
    method: type,
    data:params
  })
}
